package com.example.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.gson.Gson;
import okhttp3.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class NewsFragment extends Fragment {

    private static final int MSG_GET_NEWS = 1001;
    private String baseUrl = "https://v.juhe.cn/toutiao/index";
    private final String key = "78971bf055a3d6770a3a5e8fe3acf9ab";
    private static final String TAG="NewsFragment";
    private RecyclerView recyclerView;
    private  NewsAdapter newsAdapter;
    private List<Data> newsList=new ArrayList<>();
    private Handler handler;
    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_news,container,false);

        recyclerView=view.findViewById(R.id.recyclerView);
        newsAdapter=new NewsAdapter(newsList);
        recyclerView.setAdapter(newsAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        initHandler();
        getNewsDate();
        return view;

    }

    private void initHandler() {
        handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if(msg.what==MSG_GET_NEWS){
                    newsAdapter.changeData(newsList);
                    return true;
                }
                return false;
            }
        });
    }

    private void getNewsDate() {

        String url =baseUrl.concat("?type=").concat(type).concat("&key=").concat(key);
        OkHttpClient okHttpClient=new OkHttpClient();
        final Request request=new Request.Builder()
                .url(url)
                .get()
                .build();
        Call call =okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG,"onFailure:"+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String  jsonStr =response.body().string();
                Log.e(TAG,"onResponse:"+jsonStr);
                Gson gson=new Gson();

                NewsDataBean newsDataBean=gson.fromJson(jsonStr,NewsDataBean.class);
                newsList = newsDataBean.getResult().getData();

                handler.sendEmptyMessage(MSG_GET_NEWS);
               /* Data data =newsDataBean.getResult().getData().get(0);
                Log.e(TAG,"title:"+data.getTitle());
                Log.e(TAG,"date:"+data.getDate());
                Log.e(TAG,"Author_name:"+data.getAuthor_name());*/


            }
        });
    }
}
